package org.xiaom.butler.util;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;

import org.xiaom.butler.R;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.stream.JsonReader;

public abstract class AppHelper {
	public static final String SHARED_PREFERENCES_NAME="butler"; 
	public static final String SP_CURRENT_HOTEL="CURRENT_HOTEL"; 
	public static final String SP_CURRENT_HOTEL_PHONE="CURRENT_HOTEL_PHONE";
	public static final String USE_COUNT="USE_COUNT";
	public static void initApp(Context context){
		SharedPreferences sharedPreferences=context.getSharedPreferences(SHARED_PREFERENCES_NAME,Context.MODE_PRIVATE);
		SharedPreferences.Editor editor=sharedPreferences.edit(); 
		editor.putString(SP_CURRENT_HOTEL, "1"); 
	}
	public static void updateApp(Context context){
		NetHelper nh=new NetHelper();
		String url=NetHelper.getRootUrl(context)+context.getString(R.string.server_url_current_version);
		String data="["+nh.getString(url)+"]";
		SharedPreferences sp=context.getSharedPreferences(AppHelper.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		try {
			float cv=Float.parseFloat(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName);
			double v = 0;
			int id;
			 //解析json对象首先要生产一个JsonReader对象
            JsonReader reader=new JsonReader(new StringReader(data));
            reader.beginArray();
            while(reader.hasNext()){
                reader.beginObject();
                while(reader.hasNext()){
                    String tagName=reader.nextName();
                    if("id".equals(tagName)){
                        id=reader.nextInt();
                    }else if("version".equals(tagName)){
                    	v=reader.nextDouble();
                    }else {
                    	reader.nextString();
                    }
                }
                reader.endObject();
            }
            reader.endArray();
			if(v>cv){
				String path=context.getString(R.string.app_app_file_path);
				String name="v"+v+".apk";
				String fullName=path+name;
				FileHelper  fh=new FileHelper(context, path);
				String f=fh.createSDFile(name, new NetHelper().getInputStream(NetHelper.getRootUrl(context)+context.getString(R.string.server_update_app)));
				if(!Util.isBlank(f)){   
					File apk=new File(f);
					Intent i = new Intent(Intent.ACTION_VIEW); 
			        i.setDataAndType(Uri.fromFile(apk), "application/vnd.android.package-archive");
			        context.startActivity(i);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) { 
			e.printStackTrace();
		} catch (NameNotFoundException e) { 
			e.printStackTrace();
		}
	}  
}
